package com.bobamrz.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import com.bobamrz.domain.Document;
import com.bobamrz.hibernate.HibernateUtil;

public class DocumentDAO {
	
	private Logger log = Logger.getLogger(DocumentDAO.class);
	
	private Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	
	public Document getById(long id) {
		log.debug("retreiving doc by id="+id);
		String hql = "FROM Document WHERE document = " + id;
		Query query = session.createQuery(hql);
		List<Document> results = query.list();
		if (results.size() > 0) {
			log.debug("retreived");
			return results.get(0);
		} else {
			log.debug("not found");
			return null;
		}
	}
	
	public List<Document> getByIds(List<Long> ids) {
		String in = ids.get(0)+"";
		for(int i = 1; i<ids.size(); i++){
			in += ","+ids.get(i);
		}
		String hql = "FROM Document WHERE document IN (" + in + ")";
		Query query = session.createQuery(hql);
		List<Document> results = query.list();
		return results;
	}
	
	public void save (Document document) {
		session.merge(document);
	}
}
